#include "SudokuCell.h"
#include <QFocusEvent>
#include <QKeyEvent>
#include <QRegularExpressionValidator>

SudokuCell::SudokuCell(int row, int col, QWidget *parent)
    : QLineEdit(parent)
    , m_row(row)
    , m_col(col)
    , m_isFixed(false)
    , m_isCorrect(true)
    , m_initialValue(0)
{
    setMaxLength(1);
    setAlignment(Qt::AlignCenter);

    // 设置固定大小，确保正方形
    setFixedSize(45, 45);  // 统一宽度和高度

    // 只允许输入1-9
    QRegularExpressionValidator *validator = new QRegularExpressionValidator(QRegularExpression("[1-9]"), this);
    setValidator(validator);

    // 设置字体大小适应单元格
    QFont font = this->font();
    font.setPointSize(16);  // 调整字体大小
    font.setBold(true);
    setFont(font);

    setStyleSheet(R"(
        SudokuCell {
            border: 1px solid #cccccc;
            font-size: 16px;
            font-weight: bold;
            text-align: center;
            background-color: white;
            selection-background-color: #e6f3ff;
            border-radius: 3px;
        }
        SudokuCell:focus {
            border: 2px solid #0078d7;
            background-color: #f0f8ff;
        }
    )");

    connect(this, &QLineEdit::textChanged, this, &SudokuCell::onTextChanged);
}

void SudokuCell::setInitialValue(int value, bool isFixed)
{
    m_initialValue = value;
    m_isFixed = isFixed;
    
    if (value != 0) {
        setText(QString::number(value));
    } else {
        clear();
    }
    
  
    if (m_isFixed) {
        // 固定数字使用深色背景和粗体，明确表示不可编辑
        setStyleSheet(R"(
            SudokuCell {
                border: 2px solid #555555;
                font-size: 16px;
                font-weight: bold;
                text-align: center;
                background-color: #2c3e50;
                color: #ecf0f1;
                border-radius: 3px;
            }
        )");
        setReadOnly(true);
    } else {
        // 用户可输入的区域使用浅色背景
        setStyleSheet(R"(
            SudokuCell {
                border: 1px solid #cccccc;
                font-size: 16px;
                font-weight: bold;
                text-align: center;
                background-color: #ffffff;
                color: #2c3e50;
                selection-background-color: #e6f3ff;
                border-radius: 3px;
            }
            SudokuCell:focus {
                border: 2px solid #3498db;
                background-color: #ecf0f1;
            }
            SudokuCell:hover {
                background-color: #f8f9fa;
            }
        )");
        setReadOnly(false);
    }
    
    m_isCorrect = true;
}

void SudokuCell::setFixed(bool fixed)
{
    if (m_isFixed != fixed) {
        m_isFixed = fixed;
        emit fixedChanged(fixed);
    }
}

void SudokuCell::setCorrect(bool correct)
{
    if (m_isCorrect != correct) {
        m_isCorrect = correct;
        
          if (!m_isFixed) {
            if (!correct) {
                // 错误输入使用红色边框和浅红色背景
                setStyleSheet(R"(
                    SudokuCell {
                        border: 2px solid #e74c3c;
                        font-size: 16px;
                        font-weight: bold;
                        text-align: center;
                        background-color: #fadbd8;
                        color: #c0392b;
                        border-radius: 3px;
                    }
                    SudokuCell:focus {
                        border: 2px solid #e74c3c;
                        background-color: #f9ebea;
                    }
                )");
            } else {
                // 正确输入恢复为正常的用户输入样式
                setStyleSheet(R"(
                    SudokuCell {
                        border: 1px solid #cccccc;
                        font-size: 16px;
                        font-weight: bold;
                        text-align: center;
                        background-color: #ffffff;
                        color: #2c3e50;
                        selection-background-color: #e6f3ff;
                        border-radius: 3px;
                    }
                    SudokuCell:focus {
                        border: 2px solid #3498db;
                        background-color: #ecf0f1;
                    }
                    SudokuCell:hover {
                        background-color: #f8f9fa;
                    }
                )");
            }
        }
        
        emit correctChanged(correct);
    }
}

void SudokuCell::reset()
{
    if (!m_isFixed) {
        clear();
        setCorrect(true);
    } else {
        setText(QString::number(m_initialValue));
    }
}

void SudokuCell::focusInEvent(QFocusEvent *event)
{
    if (!m_isFixed) {
        selectAll();
    }
    QLineEdit::focusInEvent(event);
}

void SudokuCell::focusOutEvent(QFocusEvent *event)
{
    if (!m_isFixed) {
        deselect();
    }
    QLineEdit::focusOutEvent(event);
}

void SudokuCell::keyPressEvent(QKeyEvent *event)
{
    if (m_isFixed) {
        event->ignore();
        return;
    }
    
    // 处理键盘输入
    if (event->key() >= Qt::Key_1 && event->key() <= Qt::Key_9) {
        QLineEdit::keyPressEvent(event);
    } else if (event->key() == Qt::Key_Backspace || event->key() == Qt::Key_Delete || event->key() == Qt::Key_0) {
        clear();
    } else if (event->key() == Qt::Key_Left || event->key() == Qt::Key_Right || 
               event->key() == Qt::Key_Up || event->key() == Qt::Key_Down ||
               event->key() == Qt::Key_Tab || event->key() == Qt::Key_Backtab) {
        QLineEdit::keyPressEvent(event);
    } else {
        event->ignore();
    }
}

void SudokuCell::onTextChanged(const QString &text)
{
    int value = 0;
    if (!text.isEmpty()) {
        value = text.toInt();
    }
    
    emit valueChanged(m_row, m_col, value);
}
